#include "../source/agi4c.h"
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>

void agi_log(char *line);
void agi_log_result(AGI_RESULT *res);

int main(int argc, char *argv[]) {
  signal(SIGHUP, SIG_IGN);
  agi_collect_request();
  agi_set_channel(agi_request("agi_channel"));
  char *type = agi_request("agi_type");
  char *exten = agi_request("agi_extension");

  if (strcmp("SIP", type) == 0 && strcmp("h", exten) != 0) {
    AGI_RESULT *res;

	char *user = agi_get_variable("USER");
	agi_log(user);
	
	KEYVAL **choicelist = agi_makechoicelist(3);
	agi_addchoice(choicelist, 0, "1", "*Press 1 to enter prepaid card");
	agi_addchoice(choicelist, 1, "2", "*Press 2 to dial");
	agi_addchoice(choicelist, 2, "0", "*Press 3 to exit");
	char *choice = agi_menu(choicelist, 3);
	agi_log(choice);
	if (strcmp("1", choice) == 0) {
		agi_text2wave("Enter prepaid card", "*");
	} else if (strcmp("2", choice) == 0) {
		agi_text2wave("Dial", "*");
	} else if (strcmp("0", choice) == 0) {
		agi_text2wave("Exit", "*");
	} else {
		agi_text2wave("Nothing", "*");
	}

	char *filename;
	char *filename2;
	char *filename3;
	
	filename = agi_vnspeak_viqr_file("Nha^.p du+~ lie^.u");
    char *input = "";
	do {
	  res = agi_stream_file(filename, "*");
	
	  input = agi_text_input("UPPERCASE");
      agi_log(input);
	} while (strcmp("ADG", input) != 0);
	
	res = agi_vnspeak_viqr("Cha`o mu+`ng dde^'n vo+'i Hai Gi! Nha^'n 1 dde^? na.p the?. Nha^'n 2 dde^? go.i ddie^.n thoa.i\\. Nha^'n 3 dde^? thoa't.", "*");
    agi_log_result(res);
	
	filename3 = agi_vnspeak_viqr_file("Cha`o mu+`ng dde^'n vo+'i Hai Gi! Nha^'n 1 dde^? na.p the?. Nha^'n 2 dde^? go.i ddie^.n thoa.i\\. Nha^'n 3 dde^? thoa't.");
	res = agi_stream_file(filename3, "*");
    agi_log_result(res);

    res = agi_vnspeak("/home/dthoai/work/v001.txt", "*");
    agi_log_result(res);
	
    filename = agi_vnspeak_file("/home/dthoai/work/v001.txt");
	res = agi_stream_file(filename, "*");
    agi_log_result(res);

	filename2 = agi_mp3togsm_file("/home/dthoai/work/a001.mp3");
	res = agi_stream_file(filename2, "*");
    agi_log_result(res);

	res = agi_stream_file(filename3, "*");
    agi_log_result(res);
	
	res = agi_stream_file(filename, "*");
    agi_log_result(res);

	res = agi_stream_file(filename2, "*");
    agi_log_result(res);
	
	res = agi_exec_dial("SIP", "dthoai@sip.linphone.org", "", "", "");
    agi_log_result(res);
	
	res = agi_hangup("");
    agi_log_result(res);
  }

  signal(SIGHUP, SIG_DFL);
  return 0;
}

void agi_log(char *line) {
  char *logfile = "/var/log/asterisk/bridge.log";
  char output[1024];
  time_t timeofday = time(NULL);
  sprintf(output, "\n====> %s\n%s\n", ctime(&timeofday), line);
  FILE *fp = fopen(logfile, "a");
  fputs(output, fp);
  fclose(fp);
}

void agi_log_result(AGI_RESULT *res) {
  char buf[10240];
  sprintf(buf, "Command: %s\n-----\nChannel: %s\nCode: %d\nResult: %s\nData: %s\nMore: %s", res->command, res->channel, res->code, res->result, res->data, res->more);
  agi_log(buf);
}
